﻿using Microsoft.Data.SqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Text;

namespace Serives
{
    public class BaseDao
    {
        static string conStr = "Data Source=.;Initial Catalog=HRDB;Integrated Security=True";
        public static SqlDataReader SelectReader(string sql, string fileName, params SqlParameter[] ps)
        {
            SqlConnection cn = new SqlConnection(conStr);
            SqlCommand cmd = new SqlCommand(sql, cn);
            if (ps.Length > 0)
            {
                cmd.Parameters.AddRange(ps);
            }
            SqlDataReader reader = null;
            try
            {
                cn.Open();
                reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
            }
            catch (Exception ex)
            {

                WriteLog(fileName, ex);
            }
            return reader;
        }
        public static object SelectSinger(string sql, string fileName, params SqlParameter[] ps)
        {
            SqlConnection cn = new SqlConnection(conStr);
            SqlCommand cmd = new SqlCommand(sql, cn);
            if (ps.Length > 0)
            {
                cmd.Parameters.AddRange(ps);
            }
            Object obj = null;
            try
            {
                cn.Open();
                obj = cmd.ExecuteScalar();
            }
            catch (Exception ex)
            {
                WriteLog(fileName, ex);

            }
            finally
            {
                cn.Close();
            }

            return obj;
        }

        private static void WriteLog(string fileName, Exception ex)
        {
            using (StreamWriter sw = new StreamWriter("错误日志.txt", true))
            {
                sw.WriteLine("----------日志的开始-------------");
                sw.WriteLine("错误时间:" + DateTime.Now.ToString());
                sw.WriteLine("错误内容:" + ex.Message);
                sw.WriteLine("错误文件:" + fileName);
                sw.WriteLine("----------日志的结束-------------");
            }
        }
        public static int 增删改(string sql, string fileName, params SqlParameter[] ps)
        {
            SqlConnection cn = new SqlConnection(conStr);
            SqlCommand cmd = new SqlCommand(sql, cn);
            if (ps.Length > 0)
            {
                cmd.Parameters.AddRange(ps);
            }
            int result = 0;
            try
            {
                cn.Open();
                result = cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {

                WriteLog(fileName, ex);
            }
            finally
            {
                cn.Close();
            }
            return result;
        }
        public static DataTable GetData(string sql, string fileName, params SqlParameter[] ps)
        {
            SqlConnection cn = new SqlConnection(conStr);
            SqlDataAdapter ad = new SqlDataAdapter(sql, cn);
            if (ps.Length > 0)
            {
                ad.SelectCommand.Parameters.AddRange(ps);
            }
            DataTable dt = new DataTable();
            try
            {
                ad.Fill(dt);
            }
            catch (Exception ex)
            {

                WriteLog(fileName, ex);
            }
            return dt;
        }
        public static DataTable GetTable(string sql)
        {
            SqlConnection cn = new SqlConnection(conStr);
            DataSet dt = new DataSet();
            SqlDataAdapter ad = new SqlDataAdapter(sql, cn);
            try
            {
                ad.Fill(dt);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt.Tables[0];
        }
        public static int InsertUpdateDelete(string sql)
        {
            int reuslt = 0;
            using (SqlConnection cn = new SqlConnection(conStr))
            {

                SqlCommand cmd = new SqlCommand(sql, cn);
                try
                {
                    cn.Open();
                    reuslt = cmd.ExecuteNonQuery();
                }
                catch (Exception ex)
                {

                    throw ex;
                }
            }
            return reuslt;
        }
    }
}
